import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Part of the wizard-mode steps. The user is prompted 
 * as to whether they want a table of contents in their
 * generated report.
 * 
 * @author James Stanier
 */
public class TocGUI implements ActionListener {
	
	WizTitleGUI wiz6;
	
	String outputFileName;
	String outputDir;
	String paperType;
	String variableOutput;
	String title;
	String author;
	int fontSize;
	boolean twoSidedPrinting;
	boolean inDepthSection;
	boolean keywordIndex;
	boolean codeIndex;
	boolean titlePage;
	boolean tableOfContents;

	JFrame window;
	JFrame prev;
	JButton nextButton;
	JButton backButton;
	JCheckBox tocButton;

	ArrayList<File> inputFiles;
	
	/**
	 * Constructor.
	 * @param g The previous screen, WizTitleGUI.
	 */
	public TocGUI(WizTitleGUI g) {
		wiz6 = g;
		inputFiles = wiz6.getInputFiles();
		prev = wiz6.getWindow();
		outputFileName = wiz6.getOutputFileName();
		outputDir = wiz6.getOutputDir();
		paperType = wiz6.getPaperType();
		fontSize = wiz6.getFontSize();
		twoSidedPrinting = wiz6.isTwoSidedPrinting();
		inDepthSection = wiz6.isInDepthSection();
		keywordIndex = wiz6.isKeywordIndex();
		variableOutput = wiz6.getVariableOutput();
		codeIndex = wiz6.isCodeIndex();
		title = wiz6.getTitle();
		author = wiz6.getAuthor();
		titlePage = wiz6.isTitlePage();
		tableOfContents = false;
		tocWindow();
	}
	
	/**
	 * Configures and draws the screen.
	 */
	public void tocWindow() {
		window = new JFrame("JavaLiN - Wizard mode");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,500);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);
		
		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);
		
		// Heading information
		JPanel progress = new JPanel();
		JPanel header = new JPanel();
		progress.setLayout(new BorderLayout());
		header.setLayout(new BorderLayout());
		JLabel backwards = new JLabel("<HTML><font size=2>&#171; Title and author</HTML>");
		progress.add(backwards, BorderLayout.WEST);
		JLabel forwards = new JLabel("<HTML><font size=2> Review &#187;</HTML>");
		progress.add(forwards, BorderLayout.EAST);
		JLabel heading = new JLabel("<HTML><b><font size=5>7. Table of contents</b></HTML>");
		heading.setHorizontalAlignment(JLabel.CENTER);
		heading.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		header.add(heading, BorderLayout.CENTER);
		JLabel description = new JLabel("<HTML><font size=3>Do you wish a table of contents to be " +
				"generated for the entire report?.</HTML>");
		description.setBorder(BorderFactory.createEmptyBorder(20,10,10,10));
		header.add(description, BorderLayout.SOUTH);
		top.add(progress, BorderLayout.CENTER);
		top.add(header, BorderLayout.SOUTH);
		
		// Check box
		JPanel tocPanel = new JPanel();
		tocPanel.setBorder(BorderFactory.createEmptyBorder(10,0,0,10));
		tocButton = new JCheckBox("Table of contents");
		tocButton.addActionListener(this);
		tocButton.setToolTipText("Create a table of contents for the report.");
		tocPanel.add(tocButton);
		
		// Navigation buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		navButtons.add(nextButton, BorderLayout.EAST);
		
		window.add("North",top);
		window.add("Center",tocPanel);
		window.add("South",navButtons);
		window.validate();
		window.setVisible(true);
	}
	
	/**
	 * Actions to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(tocButton)) {
			tableOfContents = tocButton.isSelected();
		}
		else if(x.equals(backButton)) {
			window.setVisible(false);
			prev.setVisible(true);
		}
		else if(x.equals(nextButton)) {
			window.setVisible(false);
			ReviewGUI rgui = new ReviewGUI(this);
		}
	}
	
	/**
	 * Get the current font size.
	 * @return The font size as an integer.
	 */
	public int getFontSize() {
		return fontSize;
	}

	/**
	 * Get the current paper type.
	 * @return The paper type as a String.
	 */
	public String getPaperType() {
		return paperType;
	}

	/**
	 * Get current two-sided printing status.
	 * @return Whether two-sided printing is enabled.
	 */
	public boolean isTwoSidedPrinting() {
		return twoSidedPrinting;
	}

	/**
	 * Get the current window.
	 * @return The current JFrame window.
	 */
	public JFrame getWindow() {
		return window;
	}

	/**
	 * Get the current output directory.
	 * @return The output directory as a String.
	 */
	public String getOutputDir() {
		return outputDir;
	}

	/**
	 * Get the output file name.
	 * @return The output file name as a String.
	 */
	public String getOutputFileName() {
		return outputFileName;
	}

	/**
	 * Get whether an in-depth section has been requested.
	 * @return Whether there will be an in-depth section as a boolean.
	 */
	public boolean isInDepthSection() {
		return inDepthSection;
	}

	/**
	 * Get whether a keyword index has been requested.
	 * @return Whether there will be a keyword index as a boolean.
	 */
	public boolean isKeywordIndex() {
		return keywordIndex;
	}

	/**
	 * Get the type of variable output requested.
	 * @return Variable output status as a String.
	 */
	public String getVariableOutput() {
		return variableOutput;
	}

	/**
	 * Get whether a code index has been requested.
	 * @return Whether there will be a code index as a boolean.
	 */
	public boolean isCodeIndex() {
		return codeIndex;
	}

	/**
	 * Get the input source file references.
	 * @return Input source files as File objects.
	 */
	public ArrayList<File> getInputFiles() {
		return inputFiles;
	}

	/**
	 * Get the current author name.
	 * @return The author as a String.
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Get the current title.
	 * @return The title as a String.
	 */
	public String getTitle() {
		return title;
	}
	
	/**
	 * Get whether the user has requested a title page.
	 * @return Whether a title page is to be included.
	 */
	public boolean isTitlePage() {
		return titlePage;
	}
	
	/**
	 * Get whether the user has requested a table of
	 * contents.
	 * @return Whether a table of contents is to be included.
	 */
	public boolean isTableOfContents() {
		return tableOfContents;
	}
}
